<template>
  <a-layout>
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible>
      <div class="logo" />
      <a-menu mode="inline" theme="dark" :items="menuTree" @click="clickMenuItem"></a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <menu-unfold-outlined v-if="collapsed" class="trigger" @click="() => (collapsed = !collapsed)" />
        <menu-fold-outlined v-else class="trigger" @click="() => (collapsed = !collapsed)" />
      </a-layout-header>
      <a-layout-content :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '700px' }">
        <router-view></router-view>
      </a-layout-content>
      <a-layout-footer style="text-align: center"> Ant Design ©2018 Created by Ant UED </a-layout-footer>
    </a-layout>
  </a-layout>
</template>
<script setup>
import router from "@/router";
import { userPermissionStore } from "@/stores/user";
import * as antdIcons from "@ant-design/icons-vue";
import { h, ref } from 'vue';
const store = userPermissionStore();
const collapsed = ref(false);
function getMenu(menu) {  
  return menu.map((item) => {
    let menuItem = {
      key: item.path,
      label: item.title,
      path: item.path
    };
    if (item.icon) {
      if (antdIcons[item.icon]) {
        menuItem.icon = () => h(antdIcons[item.icon]);
      } else {
        console.warn(`图标 "${item.icon}" 不存在于antd图标库中`);
        menuItem.icon = () => h(antdIcons['AppstoreOutlined']);
      }
    }
    if (item.children && item.children.length > 0) {
      menuItem.children = getMenu(item.children);
    }
    return menuItem;
  });
}

const menuTree = getMenu(store.menuData);

const clickMenuItem = (e) => {
  router.push(e.item.path);
};
</script>
<style>
.ant-layout {
  height: 100vh;
}
.trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

.logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.3);
  margin: 16px;
}

.site-layout .site-layout-background {
  background: #fff;
}
</style>
